const Koa = require('koa'),
      Router = require('koa-router'),
      koaBody = require('koa-body'),
      fs = require('fs')
      path = require('path');

const app = new Koa(),
      router = new Router();

router.post('/upload', koaBody({multipart:true}),async (ctx,next) => {
    console.log(ctx.request.files);
    //process.exit();

    const files = ctx.request.files.file;

    for(let file of files) {
      let filePath = path.join(__dirname,'files') + `/${file.originalFilename}`;
      const readStream = fs.createReadStream(file.filepath);

      const writeStream = fs.createWriteStream(filePath);

      readStream.pipe(writeStream);
    }
    
    ctx.body = JSON.stringify(ctx.request.body);
  }
);

app.use(router.routes());

app.listen(8080);
